<script setup lang="ts">
import type { ColProps } from "@arco-design/web-vue";
import { onMounted, ref, computed } from "vue";
import { getPro, getStatus } from "@/services/teacher";
import Local from "@/utils/local";
import type { projectItem } from "@/types/teacher";
import ExpertReviewDetail from "./expertReviewDetail.vue";
import Score from "./score.vue";

const leftCol: ColProps = {
  span: 6,
  xxl: 6,
  xl: 8,
  lg: 8,
  md: 10,
  sm: 12,
  xs: 12,
};

const rightCol: ColProps = {
  span: 18,
  xxl: 18,
  xl: 16,
  lg: 16,
  md: 14,
  sm: 12,
  xs: 12,
};

const data = ref<projectItem[]>([]);
const teacherName = ref([]);
const teacherData = Local.localRead("teacher");
const role = teacherData ? JSON.parse(teacherData) : null;
const changeData = ref({});
const getList = () => {
  getPro({
    groupId: role.groupId,
    current: 1,
    size: 99999
  })
    .then((res) => {
      teacherName.value = res.data.teacherNames.split(",");
      data.value = res.data.projectList.records;
      changeData.value = data.value[0];
      setTimeout(() => {
        if (receivedVariable.value !== 1) {
          openDetail(data.value[0]);
          clickedIndex.value = 0;
        }
      }, 300);
    })
    .finally(() => {
      getTopic();
    });
};

const reviewForm = ref<{
  show: () => void;
}>();

const score = ref<{
  show: (id: string, bool: boolean) => void;
}>();

const registUid = ref("");
const type = ref("");
const clickedIndex = ref<number | null>(null);

const openDetail = (res: projectItem) => {
  registUid.value = res.registUid;
  type.value = res.projectType;
  changeData.value = res;
  reviewForm.value?.show();
};

const handleScore = () => {
  score.value?.show(data.value[clickedIndex!.value as number].registUid, isUnmarked(data.value[clickedIndex!.value as number]));
};
const receivedVariable = ref<any>(null);
const handleUpdateVariable = (value: any) => {
  receivedVariable.value = value.value;
};

const statusList = ref([]);
// 获得评分状态
const getTopic = () => {
  getStatus({
    groupId: JSON.parse(Local.localRead("teacher")).groupId,
    teacherId: JSON.parse(Local.localRead("teacher")).teacherId,
  }).then((res) => {
    statusList.value = res.data;
  });
};

onMounted(() => {
  getList();
});

// 判断项目是否未判
function isUnmarked (item: projectItem) {
  const statusItem = statusList.value.find(
    (s) => s.registUid === item.registUid
  );
  return statusItem && statusItem.markStatus == "0";
};

const handleClick = (index: number) => {
  clickedIndex.value = index;
  console.log(data.value[index])
  openDetail(data.value[index]);
};

// true
const isClicked = (index: number) => {
  return clickedIndex.value === index;
};

// 已判
const markedCount = computed(() => {
  return statusList.value.filter((s) => s.markStatus !== "0").length;
});

// 计算总项目数
const totalCount = computed(() => {
  return data.value.length;
});
</script>

<template>
  <div class="container">
    <a-row :gutter="24">
      <a-col v-bind="leftCol">
        <a-card>
          <div class="content-topic">
            <p>
              <span>当前：<span style="color: #00afff">蓝色</span></span>
              <span style="margin-left: 20px">未判：<span style="color: #ff7d00">橙色</span></span>
              <span style="margin-left: 20px">已判：<span style="color: #09be49">绿色</span></span>
              <span style="margin-left: 20px;font-weight: 500;">{{ markedCount }}/{{ totalCount }}</span>
            </p>
            <div class="newTopicListBox">
              <a-tooltip
                  v-for="(item, index) in data"
                  :key="index"
                  :content="item?.projectName"
              >
                <a-button
                    class="phone"
                    :status="isUnmarked(item) ? 'warning' : 'success'"
                    @click="handleClick(index)"
                    :class="{ 'blue-button': isClicked(index) }"
                >
                  {{index + 1}}.{{ item?.projectName }}
                </a-button>
              </a-tooltip>
            </div>
          </div>
        </a-card>
      </a-col>
      <a-col v-bind="rightCol">
        <a-card>
          <div class="content">
            <div class="content-box">
              <div>
                <a-tag
                  color="#e8ffea"
                  v-if="changeData.projectType == '0'"
                  style="margin-right: 10px;color: #00b42a;"
                >常规赛题</a-tag>
                <a-tag
                  color="#e8ffea"
                  v-else-if="changeData.projectType == '1'"
                  style="margin-right: 10px;color: #00b42a;"
                >企业子赛题</a-tag>
                <a-tag
                  color="red"
                  style="margin-right: 10px;"
                >{{ changeData.channel }}</a-tag>
                <a-tag
                  color="#ff7d00"
                  style="margin-right: 10px;"
                >{{ changeData.dataDirection }}</a-tag>
              </div>
              <div>
                <a-button
                  type="primary"
                  @click="handleScore"
                  class="btn"
                >进行评分</a-button>
              </div>
            </div>
          </div>
          <div class="">
            <expert-review-detail
                link="out"
              ref="reviewForm"
              @list="getList"
              :registUid="registUid"
              :type="type"
            />
          </div>
        </a-card>
      </a-col>
    </a-row>
  </div>

  <score
    ref="score"
    @list="getList"
    @updateVariable="handleUpdateVariable"
    :registUid="registUid"
  />
</template>

<style scoped lang="scss">
.phone {
  margin: 5px;
}
.container {
  :deep(.arco-row) {
    height: 100%;
    .arco-col,
    .arco-card {
      height: 100%;
    }
  }
  :deep(.arco-card-body) {
    height: 100%;
    box-sizing: border-box;
  }

  .content {
    width: 100%;
    // min-height: 500px;
    display: flex;
    gap: 24px;
    &-box {
      height: 100%;
      flex: 1;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    &-topic {
      height: 100%;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      //width: 30%;
    }
  }
  h3 {
    text-align: center;
  }
  .newTopicListBox {
    flex: 1;
    //display: block !important;
    display: flex;
    flex-direction: column;
    //flex-wrap: wrap;
    //align-content: flex-start;
    width: 100%;
    //height: calc(100% - 60px);
    overflow: auto;
    overflow-x: hidden;
    :deep(.arco-btn) {
      //display: inline-block;
      display: block;
      justify-content: flex-start;
      min-height: 40px;
      text-align: left;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

  }
  .timu {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    width: 100%;
    box-sizing: border-box;
    padding-right: 20%;
    p {
      padding: 0;
      margin: 0;
      line-height: 36px;
      //height: 63px;
    }
  }
}
.blue-button {
  background-color: #00afff;
  color: white;
}

.btn {
  background-color: #0b0080;
}
</style>
